﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KL.Store.Core.Domain;
using TN.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace KL.Store.Data
{
    public class dbPage
    {
        public static IList<Page> Pages { get; set; }

        #region Get

        public static string Get(int RefId)
        {
            Page page = new Page();
            page = Pages.Where(p => p.SatirId == RefId).FirstOrDefault();

            if (page == null)
            {
                Logger.Write("Reference5 Not Found, Ref5Id = " + RefId.ToString(), "Exception");
                return RefId.ToString() + " Numaralı İçerik Bulunamadı.";
            }

            return page.Yanit;
        }

        #endregion

        #region GetListContent

        public static IList<Page> GetListContent()
        {
            IList<Page> objectList = new List<Page>();

            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Ref5_List", objectList);

            return objectList;
        }

        #endregion

        #region GetListAll

        public static void GetListAll(string ImageUrl)
        {
            Pages = new List<Page>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Ref5_ListAll", Pages);
            foreach (Page page in Pages)
            {
                page.Yanit = page.Yanit.Replace("{ImageUrl}", ImageUrl);
            }
        }

        #endregion

        #region GetByName

        public static Page GetByName(string PageName)
        {
            Page page = new Page();
            page = Pages.Where(p => String.Equals(p.Url, PageName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();

            if (page == null)
            {
                Logger.Write("Reference5 Not Found, PageName = " + PageName, "Exception");
                page = new Page();
                page.Yanit = "Sayfa bulunamadı.";
            }

            return page;
        }

        #endregion

        #region GetById

        public static Page GetById(int PageId)
        {
            Page page = new Page();
            page = Pages.Where(p => p.SatirId==PageId).FirstOrDefault();

            if (page == null)
            {
                Logger.Write("Reference5 Not Found, PageId = " + PageId.ToString(), "Exception");
                page = new Page();
                page.Yanit = "Sayfa bulunamadı.";
            }

            return page;
        }

        public static Page GetByName_Old(string PageName)
        {
            IList<Page> objectList = new List<Page>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Ref5_GetByName", objectList, new object[] { PageName });
            if (objectList == null)
            {
                Logger.Write("Reference5 Not Found, PageName = " + PageName, "Exception");
            }

            return objectList.FirstOrDefault();
        }

        #endregion

        #region SavePage

        public static int SavePage(Page page)
        {
            return (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Pages_Save", new object[] { page.SatirId, page.Aciklama, page.Yanit, page.Url, page.eStoreYenile, DateTime.Now });
        }

        #endregion
    }
}
